using XDQ.VAT.Exams;
using XDQ.VAT.Courses;
using XDQ.VAT.Classes;
using XDQ.VAT.Students;
using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.IdentityServer.EntityFrameworkCore;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement.EntityFrameworkCore;

namespace XDQ.VAT.EntityFrameworkCore
{
    public static class VATDbContextModelCreatingExtensions
    {

        public static void ChangeAbpStatic()
        {
            Volo.Abp.PermissionManagement.AbpPermissionManagementDbProperties.DbSchema = "permission";
            Volo.Abp.SettingManagement.AbpSettingManagementDbProperties.DbSchema = "setting";
            Volo.Abp.BackgroundJobs.BackgroundJobsDbProperties.DbSchema = "job";
            Volo.Abp.AuditLogging.AbpAuditLoggingDbProperties.DbSchema = "auditing";
            Volo.Abp.Identity.AbpIdentityDbProperties.DbSchema = "identity";
            Volo.Abp.IdentityServer.AbpIdentityServerDbProperties.DbSchema = "identityServer";
            Volo.Abp.FeatureManagement.FeatureManagementDbProperties.DbSchema = "feature";
            Volo.Abp.TenantManagement.AbpTenantManagementDbProperties.DbSchema = "tenant";

        }

        public static void ConfigureAbp(this ModelBuilder builder)
        {
            Check.NotNull(builder, nameof(builder));
            ChangeAbpStatic();

            builder.ConfigurePermissionManagement(options =>
            {
                options.Schema = Volo.Abp.PermissionManagement.AbpPermissionManagementDbProperties.DbSchema;
            });

            builder.ConfigureSettingManagement(options =>
            {
                options.Schema = Volo.Abp.SettingManagement.AbpSettingManagementDbProperties.DbSchema;
            });

            builder.ConfigureBackgroundJobs(options =>
            {
                options.Schema = Volo.Abp.BackgroundJobs.BackgroundJobsDbProperties.DbSchema;
            });

            builder.ConfigureAuditLogging(options =>
            {
                options.Schema = Volo.Abp.AuditLogging.AbpAuditLoggingDbProperties.DbSchema;
            });

            builder.ConfigureIdentity(options =>
            {
                options.Schema = Volo.Abp.Identity.AbpIdentityDbProperties.DbSchema;
            });

            builder.ConfigureIdentityServer(options =>
            {
                options.Schema = Volo.Abp.IdentityServer.AbpIdentityServerDbProperties.DbSchema;
            });

            builder.ConfigureFeatureManagement(options =>
            {
                options.Schema = Volo.Abp.FeatureManagement.FeatureManagementDbProperties.DbSchema;
            });

            builder.ConfigureTenantManagement(options =>
            {
                options.Schema = Volo.Abp.TenantManagement.AbpTenantManagementDbProperties.DbSchema;
            });

        }

        public static void ConfigureVAT(this ModelBuilder builder)
        {
            Check.NotNull(builder, nameof(builder));

            /* Configure your own tables/entities inside here */

            //builder.Entity<YourEntity>(b =>
            //{
            //    b.ToTable(VATConsts.DbTablePrefix + "YourEntities", VATConsts.DbSchema);
            //    b.ConfigureByConvention(); //auto configure for the base class props
            //    //...
            //});




            builder.Entity<AppStudent>(b =>
            {
                b.ToTable(VATConsts.DbTablePrefix + "AppStudents", VATConsts.DbSchema);
                b.ConfigureByConvention();
                /* Configure more properties here */
            });

            builder.Entity<AppStudentClass>(b =>
            {
                b.ToTable(VATConsts.DbTablePrefix + "AppStudentClasses", VATConsts.DbSchema);
                b.ConfigureByConvention();

                b.HasOne<AppStudent>().WithMany().OnDelete(DeleteBehavior.Cascade).HasForeignKey(x => x.StudentId);
                b.HasOne<AppClass>().WithMany().OnDelete(DeleteBehavior.Cascade).HasForeignKey(x => x.ClassId);
            });
            builder.Entity<AppStudentExamScore>(b =>
            {
                b.ToTable(VATConsts.DbTablePrefix + "AppStudentExamScores", VATConsts.DbSchema);
                b.ConfigureByConvention();

                b.HasOne<AppStudent>().WithMany().OnDelete(DeleteBehavior.Cascade).HasForeignKey(x => x.StudentId);
            });

            builder.Entity<AppClass>(b =>
            {
                b.ToTable(VATConsts.DbTablePrefix + "AppClasses", VATConsts.DbSchema);
                b.ConfigureByConvention();

                /* Configure more properties here */
            });

            builder.Entity<AppExam>(b =>
            {
                b.ToTable(VATConsts.DbTablePrefix + "AppExames", VATConsts.DbSchema);
                b.ConfigureByConvention();

                /* Configure more properties here */
                b.HasOne<AppClass>().WithMany().OnDelete(DeleteBehavior.Cascade).HasForeignKey(x => x.ClassId);
                b.HasOne<AppCourse>().WithMany().OnDelete(DeleteBehavior.Cascade).HasForeignKey(x => x.CourseId);
            });


            builder.Entity<AppCourse>(b =>
            {
                b.ToTable(VATConsts.DbTablePrefix + "AppCourses", VATConsts.DbSchema);
                b.ConfigureByConvention();


                /* Configure more properties here */
            });


        }
    }
}
